<template>

  <div class="app-container">
    <el-form ref="form":model="teacherGroupInfo"label-width="120px" style="width: 500px">
      <el-form-item label="教师组名">
        <el-input v-model="teacherGroupInfo.name"></el-input>
      </el-form-item>
      <el-form-item label="学院">
        <el-select
          v-model="teacherGroupInfo.academyId"
          placeholder="请选择">
          <el-option
            v-for="academy in AcademyList"
            :key="academy.id"
            :label="academy.name"
            :value="academy.id"/>
        </el-select>
      </el-form-item>
      <el-form-item label="课程讲师">
        <el-col :span="16">
          <el-select
            v-model="CurrTeacherId"
            placeholder="请选择">

            <el-option
              v-for="teacher in teacherList"
              :key="teacher.id"
              :label="teacher.name"
              :value="teacher.id"/>
          </el-select>
        </el-col>
        <el-col :span="3">
          <el-button size="mini" icon="el-icon-plus" @click="addTeacher" circle></el-button>
        </el-col>
        <el-col :span="3">
          <el-button size="mini" icon="el-icon-delete" type="danger"@click="clearTeacher" circle></el-button>
        </el-col>
        <el-col :span="2">
          {{this.teacherNum}}
        </el-col>
      </el-form-item>

      <el-form-item label="简介">
        <el-input v-model="teacherGroupInfo.intro"></el-input>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import Academy from "../../../api/edu/Academy";
import teacherGroup from "../../../api/edu/teacherGroup";
import teacher from "../../../api/edu/teacher";
let defaultForm = {
  name: '',
  academyId:'',
  sort: 0,
  intro: '',
}

export default {
name: "add",
  data(){
    return{
      teacherNum:0,
      teacherGroupInfo:defaultForm,
      AcademyList:[],
      saveBtnDisabled:false,
      TeacherAddList:[],
      CurrTeacherId:'',
      teacherList:[],
    }

  },
  watch: {//监听 to  from路由变化方式
    $route(to, from) {
      this.init()
    }
  },
  created() {
    if (this.$route.params && this.$route.params.id) {
      const id = this.$route.params.id
      this.fetchDataById(id)
    }
    this.getAcaList()
    this.getTeacher()
  },
  methods:{
  getTeacher(){
    teacher.getListTeacher().then(res=>{
      this.teacherList = res.data.teachers
    })
  },
    addTeacher(){
      let flag=0
      if(this.TeacherAddList){
        for(let i=0;i<this.TeacherAddList.length;i++) {
          if (this.TeacherAddList[i] == this.CurrTeacherId) {
            flag=1
            break
          }
        }
      }
      if(flag==0&&this.CurrTeacherId){
        this.TeacherAddList.push(this.CurrTeacherId)
        this.teacherNum++
      }
      flag=0
    },
    clearTeacher(){
      this.TeacherAddList=[]
    },
    getAcaList(){
      Academy.getAllAcaList().then(res=>{
        this.AcademyList=res.data.acaList
      })
    },
    init() {
      if (this.$route.params && this.$route.params.id) {
        const id = this.$route.params.id
        this.fetchDataById(id)
      } else {
        // 使用对象拓展运算符，拷贝对象，而不是引用，
        // 否则新增一条记录后，defaultForm就变成了之前新增的teacher的值
        this.teachergroup = { ...defaultForm }
      }
    },
    updateData() {
      this.saveBtnDisabled = true
      teacherGroup.updateTeacherGroup(this.teachergroup).then(response => {
        return this.$message({
          type: 'success',
          message: '修改成功!'
        })
      }).then(resposne => {
        this.$router.push({ path: '/teacherGroup/teacherGrouplist' })
      }).catch((response) => {
        // console.log(response)
        this.$message({
          type: 'error',
          message: '保存失败'
        })
      })
    },
    fetchDataById(id) {
      teacherGroup.getByTeacherGroupById(id).then(response => {
        this.teachergroup = response.data.teachergroup
      }).catch((response) => {
        this.$message({
          type: 'error',
          message: '获取数据失败'
        })
      })
    },
    saveOrUpdate() {
      this.saveBtnDisabled = true
      if (!this.teachergroup.id) {
        this.saveData()
      } else {
        this.updateData()
      }
    },
    // 保存
    saveData() {
      teacherGroup.add(this.teacherGroupInfo,this.TeacherAddList).then(response => {
        defaultForm={}
        return this.$message({
          type: 'success',
          message: '保存成功!'
        })

      }).then(resposne => {
        this.$router.push({ path: '/teacherGroup/teacherGrouplist' })
      }).catch((response) => {
        // console.log(response)
        this.$message({
          type: 'error',
          message: '保存失败'
        })
      })
    }
  }

}
</script>

<style scoped>

</style>
